package com.wang.myapplication.base;

import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xhwang on 2018/4/27.
 */

public abstract class MyBaseAdapter<T> extends BaseAdapter {
    private List<T> mDataLists = new ArrayList<>();//为什么不是private List<T> mDataLists; 为了初始化时,这个是不能为null的,下面getItem getCount都需要。所以设置个默认0
    //private Context mContext;//是否必须呢
    private int layoutId;

    public MyBaseAdapter() {
    }

    public MyBaseAdapter(int pLayoutId) {
        layoutId = pLayoutId;
    }

    public MyBaseAdapter(List<T> pDataLists, int pLayoutId) {
        mDataLists = pDataLists;
        layoutId = pLayoutId;
    }

    public List<T> getDataLists() {
        return mDataLists;
    }


    public void addDataLists(List<T> pDataLists) {
        if (pDataLists != null) {
            mDataLists.addAll(pDataLists);
            notifyDataSetChanged();
        }
    }

    public void replaceDataList(List<T> dataList) {
        if (dataList != null) {
            mDataLists.clear();
            mDataLists.addAll(dataList);
        } else {
            mDataLists.clear();
        }
        notifyDataSetChanged();
    }

    //添加一个元素
    public void add(T data) {
        if (mDataLists == null) {
            mDataLists = new ArrayList<>();
        }
        mDataLists.add(data);
        notifyDataSetChanged();
    }

    //往特定位置，添加一个元素
    public void add(int position, T data) {
        if (mDataLists == null) {
            mDataLists = new ArrayList<>();
        }
        mDataLists.add(position, data);
        notifyDataSetChanged();
    }


    public boolean removeData(T item) {
        boolean result = false;
        if (item != null) {
            mDataLists.remove(item);
            notifyDataSetChanged();
            result = true;
        }
        return result;
    }

    public boolean removeData(int index) {
        boolean result = false;
        if (index >= 0 && index < mDataLists.size()) {
            mDataLists.remove(index);
            notifyDataSetChanged();
            result = true;
        }
        return result;
    }

    public void clear() {
        if (mDataLists != null) {
            mDataLists.clear();
        }
        notifyDataSetChanged();
    }





    @Override
    public int getCount() {
        return mDataLists.size();
    }

    @Override
    public Object getItem(int position) {
        return mDataLists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
            viewHolder = new ViewHolder(convertView, position);
        } else
            viewHolder = (ViewHolder) convertView.getTag();

        handItemView(viewHolder, mDataLists.get(position));
        return convertView;
    }

    /**
     * 子类必须重写的方法，通过这个方法初始化holder中的View即可
     */
    public abstract void handItemView(ViewHolder holder, T t);


    protected static class ViewHolder {
        private View convertView;
        private SparseArray<View> views;
        private int position;//游标

        private ViewHolder(View convertView, int pPosition) {
            this.views = new SparseArray<View>();
            this.convertView = convertView;
            this.position = pPosition;
            convertView.setTag(this);
            //ButterKnife.bind(this,convertView);//思考到底如何用
        }

        @SuppressWarnings("unchecked")
        public <T extends View> T findViewById(int id) {
            View view = views.get(id); // 同id的控件可重复使用，无需再次findViewById
            if (view == null) {
                view = convertView.findViewById(id);
                views.append(id, view); // 将此id的控件添加进sparseArray
            }
            return (T) view;
        }

        /**
         * 获取当前条目
         */
        public View getItemView() {
            return convertView;
        }

        /**
         * 获取条目位置
         */
        public int getItemPosition() {
            return position;
        }
    }


}
